﻿using INTEX.DataBase;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Linq;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    //
    public partial class YGRABDOCData
    {
        int ABAID = 1;
        /// <summary>
        /// 1.自动生产生产单所需要的必要数据
        /// 2.利用ICC_ABAX表明生产单之间的计划关系
        /// 3.产生原料需求单
        /// 4.应当根据留位的原料的变量产生
        /// </summary>
        /// <param name="dwInput"></param>
        /// <param name="src"></param>
        /// <returns></returns>
        public DataSet PrePlan(SETINTEXDB db, DataRow dwVKP, bool IsRepair)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataRow dwABAN;
            string KRS, LFD, POS;
            DataTable tbMU = db.ICC_ABAX;

            string filter = "";
            DataRow[] rows;
            KRS = dwVKP["ABPAUFKRS"].ToString();
            LFD = dwVKP["ABPAUFLFD"].ToString();
            POS = dwVKP["ABPAUFPOS"].ToString();
            decimal SOQTY = Convert.ToDecimal(dwVKP["ABPSOLLMENGE5"].ToString());
            decimal PDQTY = Convert.ToDecimal(dwVKP["ABPSOLLMENGE7"].ToString());
            decimal STKLG = Convert.ToDecimal(dwVKP["ABPSTKLG"].ToString());
            filter = "GAAUFKRS='" + KRS + "' AND GAAUFLFD='" + LFD + "' AND GAAUFPOS='" + POS + "' AND GASA='713'";
            string ABKKDAUFTNR = "";
            foreach (DataRow row in db.Tables["ABKOPF"].Rows)
            {
                ABKKDAUFTNR = row["ABKKDAUFTNR"].ToString();
            }
            rows = db.GAAUF.Select(filter);
            filter = "TRIM(GABEM)='*HX*'";
            bool InRepaire = false;
            DataRow[] RHX = db.GAAUF.Select(filter);
            if (RHX.Length > 0)
            {
                InRepaire = true;
            }
            foreach (DataRow dwAUF in rows)
            {
                IsRepair = false;
                if (dwAUF["GABEM"].ToString().Trim() == "*HX*")
                    IsRepair = true;
                if (InRepaire)
                {
                    if (!IsRepair) continue;
                }
                dwABAN = tbMU.Rows.Add();
                dwABAN["ABAID"] = ABAID++;
                dwABAN["ABAMANDANT"] = dwAUF["GAMANDANT"];
                dwABAN["ABAAUFTYP"] = "N";
                dwABAN["ABAVKAUFTYP"] = "V";
                if (IsRepair)
                {
                    dwABAN["ABAAUFKRS"] = KRS;
                    dwABAN["ABAAUFLFD"] = LFD;
                }
                else
                {
                    dwABAN["ABAAUFKRS"] = "FP";
                }
                dwABAN["ABAVKAUFKRS"] = KRS;
                dwABAN["ABAVKAUFLFD"] = LFD;
                dwABAN["ABAVKAUFPOS"] = POS;
                dwABAN["EXART"] = ABKKDAUFTNR;
                dwABAN["ABAVKUPOS"] = dwAUF["GAPOS"].ToString();
                if (dwABAN["ABAVKUPOS"].ToString().Trim() == "00")
                {
                    dwABAN["ABAVKUPOS"] = "01";
                }
                dwABAN["ABAAUFMSL"] = dwAUF["GAAUFMSL"].ToString().ToUpper(); //GAAUFMSL
                dwABAN["ABAPACKTYP"] = "BZL ";
                dwABAN["ABAHUELSE"] = " ";
                decimal GAKG1 = Convert.ToDecimal(dwAUF["GAKG1"].ToString());
                decimal GAKG2 = Convert.ToDecimal(dwAUF["GAKG2"].ToString());
                decimal GAKG3 = Convert.ToDecimal(dwAUF["GAKG3"].ToString());
                decimal GAKG5 = Convert.ToDecimal(dwAUF["GAKG5"].ToString());
                decimal GASP1 = Convert.ToDecimal(dwAUF["GASP1"].ToString());
                decimal KG1 = System.Math.Round(GAKG1, 2);
                decimal KG2 = System.Math.Round(GAKG3 + GAKG5, 2);
                decimal SP1 = Convert.ToDecimal(dwAUF["GASP1"].ToString()) / 1000 / 100;
                decimal RATE = SP1 * Math.Round(SOQTY * STKLG / 10000000 / PDQTY, 3);
                decimal RQTY = System.Math.Round(RATE * PDQTY, 0);
                if (GAKG2 > 0)
                {
                    if (!IsRepair)
                    {
                        throw new Exception("纱线:" + dwAUF["GAART"].ToString().Trim() + "-" + dwAUF["GAVAR"].ToString().Trim() + "-" + dwAUF["GAFARBE"].ToString().Trim() + "-" + dwAUF["GAAUFMSL"].ToString() + ",已经分盘，禁止二次进行");
                    }
                }
                if (KG1 != KG2)
                {
                    if (!IsRepair)
                    {
                        throw new Exception("纱线:" + dwAUF["GAART"].ToString().Trim() + "-" + dwAUF["GAVAR"].ToString().Trim() + "-" + dwAUF["GAFARBE"].ToString().Trim() + "-" + dwAUF["GAAUFMSL"].ToString() + ",留位数量" + KG2 / 100 + "不等于需求数量" + KG1 / 100);
                    }
                }
                if ((RQTY - KG1) > 10 || (RQTY - KG1) < -10)
                {
                    if (!IsRepair)
                    {
                        throw new Exception("纱线:" + dwAUF["GAART"].ToString().Trim() + "-" + dwAUF["GAVAR"].ToString().Trim() + "-" + dwAUF["GAFARBE"].ToString().Trim() + "-" + dwAUF["GAAUFMSL"].ToString() + ",按投料比计算需求" + RQTY / 100 + "KG，与需求量" + KG1 / 100 + "不同，请重新计算留位");
                    }
                }
                dwABAN["ABARESMG"] = (GAKG3 + GAKG5) * 10;
                if (IsRepair)
                {
                    dwABAN["ABARESMG"] = (GAKG1) * 10;
                }
                dwABAN["ABAQTYRECEIVED"] = (GAKG5) * 10;
                dwABAN["ABASANZSTK"] = 1;
                dwABAN["ABASSTKGEW"] = dwABAN["ABARESMG"];
                dwABAN["ABAVKART"] = dwABAN["ABARESMG"];
                dwABAN["ABAVKFARBE"] = "100000 ";
                dwABAN["ABACOLORREF"] = Helpers.Helper.Left(dwAUF["GABEM"].ToString().PadRight(10), 10);
                dwABAN["ABAWAKZ"] = "G";
                dwABAN["ABAART"] = dwAUF["GAART"];
                dwABAN["ABAVAR"] = dwAUF["GAVAR"];
                dwABAN["ABAFARBE"] = dwAUF["GAFARBE"];
                dwABAN["ABAVKSTARTD"] = dwAUF["GADATBIS"];
                dwABAN["ABALO"] = dwAUF["GALO"];
                dwABAN["ABALB"] = dwAUF["GALB"];
                dwABAN["ABALP"] = dwAUF["GALP"];
                dwABAN["ABAUSER"] = app.User;
                dwABAN["ABADATE"] = date;
                if (IsRepair)
                {
                    break;//每次仅处理一个回修任务，处理完毕之后，将回修标志删除
                }
            }
            filter = "ABAAUFTYP='N' AND ABAWAKZ='G'";
            rows = tbMU.Copy().Select(filter);
            if (rows.Length > 0)
            {
                foreach (DataRow dwABA in rows)
                {
                    SResult = ABP.PlanFPAB50(db, dwABA, ref ABAID); 
                    SReturn.Merge(SResult);
                }
            }
            return SReturn;
        }

    }
}